var nodemailer = require("nodemailer");
var config = require("./config");
var publicIp = require("public-ip")
var myPublicIP = "60.177.236.27";
function sendEmail()
{
    var transporter = nodemailer.createTransport({
        host:config.smtp,
        port: 465,
        secure: true,
        auth:{
            user:config.mailFrom,
            pass:config.mailPwd
        }
    });
    var mailOptions = {
        from:config.mailFrom,
        to:config.mailTo,
        subject:config.title,
        text:""
    }
    publicIp.v4().then((ip)=>{
        if(myPublicIP != ip)
        {
            myPublicIP = ip;
            mailOptions.text = ip;
            try{
                transporter.sendMail(mailOptions,function(err,info){
                    if(err){
                        console.log("send Fail",err);
                        return;
                    }
                    console.log("send Success:",ip);
                });
            }catch(err){
                console.log("try error",err);
            }
        }else{
            console.log("public ip no change:",ip)
        }
        //console.log("public ip:",ip);
    })  
}
setInterval(sendEmail,1000*60*60);
//sendEmail()